/**
 * Copyright (c) 2007,中企动力华南研发中心
 * All rights reserved.
 */
package com.ce.pms.tag;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.common.service.ISysrefcodeService;
import com.ce.pms.common.service.impl.SysrefcodeServiceImpl;
import com.sitechasia.webx.core.utils.context.ApplicationContext;

/**
 * 功能描述：系统参数表单选按钮
 * @author 罗军林 创建日期：2008-5-29
 */
public class SysrefcodeRadioTag extends TagSupport {
	//以下为标签对应的属性，除code、selectValue为自定义属性外，其它均为input标签属性
	private String id;
	private String name;
	private String style;
	private String className;
	private String tabIndex;
	private String onchange;
	private String onclick;

	private String selectValue;
	private String code;
	private String filterStr;


	@Override
	public int doStartTag() throws JspException {
		try {
			WebApplicationContext context = getContext();
			ISysrefcodeService sysrefcodeService=(ISysrefcodeService)context.getBean("sysrefcodeService");
			if(this.code==null)throw new Exception("The 'code' attrib must be in! ");
			List list = getDataList(sysrefcodeService);
			generationSelectHTML(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}

	/**
	 * Function:<p>根据code和filterStr来判断调用哪个service方法</p>
	 * 			<p>如果filterStr为空或为空格则根据code进行查询
	 * @param sysrefcodeService
	 * @return
	 * @throws Exception
	 * Create author:罗军林
	 * Create on:2008-6-11
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	private List getDataList(ISysrefcodeService sysrefcodeService) throws Exception {
		List list;
		if(this.code==null)throw new Exception("The 'code' attrib must be in! ");
		if(this.filterStr==null || this.filterStr.trim().equals("")){
			list=sysrefcodeService.querySysrefCodesByCode(this.code);
		}else{
			list=sysrefcodeService.querySysrefCodesByCode(this.code,this.filterStr);
		}
		return list;
	}

	/**
	 * Function:取得web上下文环境
	 * @return
	 * Create author:罗军林
	 * Create on:2008-5-9
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	private WebApplicationContext getContext() {
		ServletContext servletContext= this.pageContext.getServletContext();
		WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
		return context;
	}

	/**
	 * Function:根据list内容动态输出该HTML脚本。<br>
	 * id的生成较特殊，是根据id名再加索引值来构成的,<br>
	 * 控件的value值为SysrefcodeDO的主键,控件的code值为SysrefcodeDO的value;<br>;
	 * <p>示例：
	 *		<div id="sex">
	 *			<INPUT TYPE="radio"  name="sex" id="sex0" class="validate-one-required" tabIndex="2" code="1"  value="17">男&nbsp;</INPUT>
	 *			<INPUT TYPE="radio"  name="sex" id="sex1" class="validate-one-required" tabIndex="2" code="0"  value="18">女&nbsp;</INPUT>
	 *		</div>
	 *</p>
	 * @param list
	 * @throws Exception
	 */
	private void generationSelectHTML(List list) throws Exception {
		StringBuffer out=new StringBuffer();

		int i=0;//用于组合ID
		out.append("<span id=\""+this.id+"\">\n");
		for( Iterator<SysrefcodeDO> item=list.iterator();item.hasNext(); ){
			SysrefcodeDO itemDO=item.next();

			out.append("<INPUT TYPE=\"radio\" ");
			if(this.name!=null) 		out.append(" name=\""+this.name+"\"");
			else throw new Exception("The 'name' attrib not null! ");
			if(this.id!=null)	out.append(" id=\""+this.id+(i++)+"\"");
			if(this.className!=null)	out.append(" class=\""+this.className+"\"");
			if(this.onchange!=null)		out.append(" onchange=\""+this.onchange+"\"");
			if(this.onclick!=null)		out.append(" onclick=\""+this.onclick+"\"");
			if(this.tabIndex!=null ) 	out.append(" tabIndex=\""+this.tabIndex+"\"");
			if(this.style!=null) 		out.append(" style=\""+this.style+"\"");
			out.append(" code=\""+itemDO.getValue()+"\" ");
			out.append(" value=\""+itemDO.getId()+"\"");
			if(this.selectValue !=null && this.selectValue.equals( String.valueOf(itemDO.getId()) ))
				out.append("checked=\"checked\" ");
			out.append(">"+itemDO.getName()+"&nbsp;");
			out.append("</INPUT>\n");
		}
		out.append("</span>\n");
		pageContext.getOut().write(out.toString());
	}

/******************************************************************************************************/
	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getOnchange() {
		return onchange;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	public String getSelectValue() {
		return selectValue;
	}

	public void setSelectValue(String selectValue) {
		this.selectValue = selectValue;
	}

	public String getTabIndex() {
		return this.tabIndex;
	}

	public void setTabIndex(String tableIndex) {
		this.tabIndex = tableIndex;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getFilterStr() {
		return filterStr;
	}

	public void setFilterStr(String filterStr) {
		this.filterStr = filterStr;
	}

	public String getOnclick() {
		return onclick;
	}

	public void setOnclick(String onclick) {
		this.onclick = onclick;
	}

}
